 var  rm = null;
$.ajaxSetup({
	type: "GET"
	,cache: false
	,dataType: "json"
	,contentType:"application/json; charset=ISO-8859-1"
	,url:"ws"
	,error:function(x,e,s){
		var errotxt = "Unknow Error";
		if (x.status == 0) {
			errotxt = 'You are offline!! Please Check Your Network.';
		} else if (x.status == 404) {
			errotxt = 'URL not found.';
		} else if (x.status == 500) {
			errotxt = 'Internal Server Error.';
		} else if (e == 'timeout') {
			errotxt = 'Request Time out.' ;
		} else if (e == 'parsererror') {			
			errotxt ='Error. Parsing JSON Request failed.';
		}else{
			errotxt = 'Unknow Error.' + x.responseText;
		}
		/*
		var idrequest_t = this.data.substring(this.data.indexOf("idRequest")+10);
		idrequest_t = idrequest_t.substring(0,idrequest_t.indexOf("&"));
		//alert('idRequest: '+idrequest_t+'\nerro: '+errotxt);
		var task = $("#uid_"+idrequest_t)[0]["dta"];				
		task.imgReload.htmlX.attr("src","lib/org/underas/assets/img16x16/change-icon.png");
		task.erroState(errotxt);
		*/
	}      
});

function RequestManager(){   	
	this.timeToEnd = null;
	//this.imgClock = new Img("lib/org/underas/assets/img54x54/relogio.png");
	//this.imgClock.htmlX.css({"float":"left","width":"42px","margin-right":"4px"});
	this.pbPrincipal = new ProgressBar("-");	
	this.pbPrincipal.htmlX.css({'margin-top':'4px'});
	this.imgShowHide = new BoxImg("lib/org/underas/assets/img16x16/icone_nextn.png");
	this.imgShowHide.htmlX.css({'width':'100%','margin-bottom':'4px','height':'7px'});
	this.boxTasks = new VBox();		
	this.boxTasks.htmlX.addClass("taskBox").css({'width':'100%','margin-bottom':'4px','height':'auto','min-height':'70px'});
	
	TitleWindow.apply(this,["Solicitacoes"]);

        //this.uid = 999;
	this.htmlX.addClass("RequestManager").css("z-index",6999);//.attr("id","uid_"+this.uid);
	
        this.htmlX[0]["dta"] = this;
	this.imgShowHide.htmlX.click($.proxy(this.mostrar,this));
	
	//this.htmlX.find('.titulow').append(this.imgClock.htmlX);
	this.append(this.pbPrincipal);	
	this.imgShowHide.htmlX.addClass("showHide");
	//this.appendChildX(this.imgShowHide);
	this.boxTasks.appendChildX(this.imgShowHide);
	this.append(this.boxTasks);
	
/*
	this.show = function(on,popup){
		this.htmlX.modal({'show':on});
	};
*/
	this.htmlX.find(".close").click(function(){
		rm.boxTasks.htmlX.find(".terro").remove();		
	});	

}

RequestManager.prototype.finalizar = function(){
    this.show(false,false);
    this.pbPrincipal.setProgress(0);
    this.htmlX.find(".finalizado").remove();
};
RequestManager.prototype.addRequest = function(req){	
	if(this.timeToEnd!=null){
		window.clearTimeout(this.timeToEnd);
	}	
	req.url = req.url||"ws";
	req.format = req.format||"json";
	this.show(true,true);
	var tsk = new Task(req);	
	this.boxTasks.appendChildX(tsk);	
	var tmtTodos = this.boxTasks.htmlX.find(".Task").size();
	this.boxTasks.htmlX.css({'height':(tmtTodos*60)+'px'});
	tsk.pbTask.setProgress(40);
    var req2 = {"idRequest":tsk.idRequest,"t":tsk.t,"s":tsk.service};
    for(var key in req){
    	if(key!="onLoad"&&req[key]!=""&&key!="url"&&req[key]!="null"){
    		req2[key]=req[key];
    	}    	
    };    
    $.ajax({
		"url":req.url,
	    "data":req2,
		"cross-origin":true,
		"dataType":req.format,
	    "success":function(dtr){	    	
			if(dtr.erro.length==0){
				$("#uid_"+dtr.idRequest)[0]["dta"].finalizar(dtr);
			}else{
				$("#uid_"+dtr.idRequest)[0]["dta"].erroState("erros in server: "+dtr.erro);
			}
			
	    }
    }); 
    
 };
 
  RequestManager.prototype.refresh = function(){  
	var tmFinalizados = this.boxTasks.htmlX.find(".finalizado").size();
	var tmtTodos = this.boxTasks.htmlX.find(".Task").size();
	var par =  (tmFinalizados/tmtTodos)*100;
	par = parseInt(par);	
	this.pbPrincipal.setProgress(par);	
	if(par==100){
		//acabou oooooo, acabou ooooo
		this.timeToEnd = window.setTimeout(function(){
			rm.finalizar();
		},1300);		
	}
	//alert(tmFinalizados+"/"+tmtTodos+"  --> "+par+"%");
 };
 
 RequestManager.prototype.mostrar = function(){ 
	/*
	if(this.boxTasks.htmlX.hasClass("requestVisible")){
		this.boxTasks.htmlX.stop().animate({"height":"15px"}).removeClass("requestVisible").find(".showHide").attr("src","lib/org/underas/assets/img16x16/icone_nextn.png");
	}else{
		this.boxTasks.htmlX.addClass("requestVisible").css({"height":"auto"}).find(".showHide").attr("src","lib/org/underas/assets/img16x16/icone_nextnd.png");
	}
	*/	
 };
 

 $(function(){
	rm = new RequestManager();
	$("body").append(rm.htmlX); 
});
